package wps.component.elementsetting {

import flash.events.Event;
import flash.events.FocusEvent;

import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;

import spark.components.NumericStepper;

import wps.skin.SettingNumericStepperSkin;
import wps.util.EnableChecker;

public class SettingNumericStepper extends NumericStepper implements ISettingItem {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var _target:Object;

    private var _targetProperty:String;

    private var _targetStyle:String;

    private var _needCopy:Boolean = false;

    //----------------------------------
    // inputComplete
    //----------------------------------

    private var _inputComplete:Boolean;

    /**
     * 输入结束后设置为true。失去焦点时，如果已经按过回车键，避免重复调用输入完成的方法
     */
    public function get inputComplete():Boolean {
        return _inputComplete;
    }

    public function set inputComplete(inputComplete:Boolean):void {
        _inputComplete = inputComplete;
    }

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    public function SettingNumericStepper() {
        super();

        setStyle("skinClass", Class(SettingNumericStepperSkin));
		
		addEventListener(FlexEvent.INITIALIZE, initializeHandler);
        addEventListener(Event.CHANGE, changeHandler);
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    override protected function partAdded(partName:String, instance:Object):void {
        super.partAdded(partName, instance);

        if (instance == textDisplay) {
            var sti:SettingTextInputBase = SettingTextInputBase(textDisplay);
            sti.integerOnly = true;
            sti.minValue = minimum;
            sti.maxValue = maximum;
        }
    }

    public function get needCopy():Boolean {
        return _needCopy;
    }

    public function set needCopy(needCopy:Boolean):void {
        _needCopy = needCopy;
    }

    [Bindable]
    public function get target():Object {
        return _target;
    }

    public function set target(target:Object):void {
        _target = target;
        targetChanged();
    }

    public function get targetProperty():String {
        return _targetProperty;
    }

    public function set targetProperty(targetProperty:String):void {
        _targetProperty = targetProperty;
        targetPropertyChanged();
    }

    public function get targetStyle():String {
        return _targetStyle;
    }

    public function set targetStyle(targetStyle:String):void {
        _targetStyle = targetStyle;
        targetStyleChanged();
    }

    public function checkEnable():void {
		var checker:EnableChecker = new EnableChecker(this);
		enabled = checker.getEnableValue();
    }

    private function targetChanged():void {
        checkEnable();
        targetPropertyChanged();
        targetStyleChanged();
    }

    private function targetPropertyChanged():void {
        if (target && targetProperty) {
            BindingUtils.bindSetter(textSetter, target, targetProperty);
        }
        checkValue();
    }

    protected function textSetter(value:Object):void {
        if (target && targetProperty) {
            this.value = target[targetProperty];
        }
    }

    private function targetStyleChanged():void {
        // 当样式对目标有效时才显示值
        if (target && enabled) {
            // 只有是用来设置样式的输入框时才读取样式，否则会覆盖targetProperty的值
            if (targetStyle) {
                value = target.getStyle(targetStyle);
            }
        }
        checkValue();
    }

    private function checkValue():void {
        value = enabled ? value : 0;
		value = isNaN(value) ? 0 : value;
    }

    protected function inputCompleted():void {
        if (target) {
            if (targetProperty) {
                target[targetProperty] = value;
            } else if (targetStyle) {
                target.setStyle(targetStyle, value);
            }
        }
    }

    //--------------------------------------------------------------------------
    //
    // Event handlers
    //
    //--------------------------------------------------------------------------

    protected function changeHandler(e:Event):void {
        inputCompleted();
        inputComplete = true;
    }

    // textDisplay.focusEnabled = false，所以需要处理focusOut
    override protected function focusOutHandler(e:FocusEvent):void {
        super.focusOutHandler(e);

        // 如果在失去焦点时已完成输入，不做操作
        if (inputComplete) {
            return;
        }
        inputCompleted();
    }

    // textDisplay.focusEnabled = false，所以需要处理focusIn
    override protected function focusInHandler(e:FocusEvent):void {
        super.focusInHandler(e);

        inputComplete = false;
    }
	
	private function initializeHandler(e:FlexEvent):void {
		enabled = target ? true : false;
	}

}

}
